//
//  BaseViewController.swift
//  weile
//
//  Created by LKM on 16/10/8.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit
import FDFullscreenPopGesture
import SnapKit

class BaseViewController: UIViewController {

    private var content_bg:UIImageView?;
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = R.colors.bg_global;
        Handler.post {
            //让它能在 加载完视图后 有时间设置是否要背景
            if self.isShowContentBg == nil {
                self.isShowContentBg = true;//默认是有背景
            }
        }
        
        //view 一般xib中实例化的话  这个就马上给调用了的
        Handler.post {
            //粗发一下
            self.isNavigationBarOpacitas = Bool(self.isNavigationBarOpacitas)
        }
        // Do any additional setup after loading the view.
    }
    //得在视图准备好之后
    public var isTransparentBg : Bool?{
        didSet{
            
            let iss  = isTransparentBg ?? false
            isShowContentBg = !iss
            
            if iss {
                view.backgroundColor = UIColor.clear;
            }else{
                view.backgroundColor = R.colors.bg_global;
            }
        }

    }
    
    public var isShowContentBg :Bool?{
        
        didSet{
            
            let iss  = isShowContentBg ?? false
            if iss {
                
                if(content_bg != nil){
                    return;
                }
                
                let bg = UIImageView(image: UIImage(named: "bg_content"))
                bg.contentMode = UIViewContentMode.scaleAspectFill
                //        view.addSubview(bg);
                view.insertSubview(bg, at: 0)
                content_bg = bg;
                
                bg.snp.makeConstraints { (cm:ConstraintMaker) in
                    cm.top.equalToSuperview()//.offset(30);
                    cm.left.equalToSuperview()//.offset(30);
                    cm.right.equalToSuperview()//.offset(-30);
                    cm.bottom.equalToSuperview()//.offset(-30);
                }
                
//                navigationController?.fd_prefersNavigationBarHidden = true
            }else{
//                if(content_bg == nil){
//                    return;
//                }
                content_bg?.removeFromSuperview();
                content_bg = nil;
                
            }
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if content_bg != nil{
            view.sendSubview(toBack: content_bg!)
        }
    }
    
    //导航栏不透明 默认透明
    var isNavigationBarOpacitas:Bool=false{
        
        didSet{
            //            if(isNavigationBarOpacitas == nil){
            //                return;
            //            }
            if(!self.isViewLoaded){
                return
            }
            if(isNavigationBarOpacitas){
               ( navigationController as? BaseNavigationController)?.navigationBarToOriginal();
            }else{
                ( navigationController as? BaseNavigationController)?.navigationBarToTransparent();
            }
            
        }
    }

    //死掉的时候透明，因为默认是透明的
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        //默认是透明的。所以要他透明
        if(isNavigationBarOpacitas){
           ( navigationController as? BaseNavigationController)?.navigationBarToTransparent()
        }
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func finish(animated:Bool = false ){
        if UIApplication.shared.isNetworkActivityIndicatorVisible {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
        }

        if(navigationController != nil){
            if navigationController!.viewControllers.count == 1 {
                navigationController!.viewControllers[0] .dismiss(animated: true, completion: nil)
                //最后一个视图没了 自己也会消失
            }else{
                //最后一个视图的pop不掉的
                navigationController!.popViewController(animated: true)
            }
            
            //最后一个视图的pop不掉的
//            navigationController?.popViewController(animated: animated)
        }else{
            self.dismiss(animated: animated, completion: nil)
        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
